package service.impl;

import java.sql.Timestamp;
import java.util.List;

import common.impl.DateHandle;
import common.impl.exception.ServiceException;

import model.MExam;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import service.interf.IExam;
import dao.interf.IExamDao;
import dao.interf.ITeacherDao;
import dao.interf.ITestDao;
import dao.pojo.Exam;
import dao.pojo.Teacher;
import dao.pojo.Test;

public class ExamImpl implements IExam {
	private IExamDao examDao;
	private ITeacherDao teacherDao;
	private ITestDao testDao;

	public void setExamDao(IExamDao examDao) {
		this.examDao = examDao;
	}

	public void setTeacherDao(ITeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}
	
	public void setTestDao(ITestDao testDao) {
		this.testDao = testDao;
	}
	
	@Override
	public String add(MExam model) {
		Exam exam = new Exam();
		
		Teacher tea = teacherDao.getById(model.getTeaId());
		if (tea == null) {
			throw new ServiceException("ERR-0021");
		} else {
			exam.setTeacher(tea);
		}
		Test test = testDao.getById(model.getTestId());
		if (test == null) {
			throw new ServiceException("ERR-0051");
		} else {
			exam.setTest(test);
		}
		
		exam.setExamName(model.getExamName());
		exam.setExamClasses(model.getExamClasses());
		exam.setExamBeginDate(model.getExamBeginDate());
		exam.setExamEndDate(model.getExamEndDate());
		exam.setExamFullScore(model.getExamFullScore());
		exam.setExamPassScore(model.getExamPassScore());
		exam.setExamHour(model.getExamHour());

		exam.setExamCreateDate(new Timestamp(System.currentTimeMillis()));

		return examDao.add(exam);
	}

	@Override
	public String getById(MExam model) {
		Exam exam = examDao.getById(model.getExamId());
		if (null == exam) {
			throw new ServiceException("ERR-0111");
		}		
		return createExamObj(exam).toString();
	}

	@Override
	public String deleteById(Integer id) {
		examDao.deleteById(id);
		return "1";
	}

	@Override
	public String edit(MExam model) {
		Exam exam = examDao.getById(model.getExamId());
		if (null == exam) {
			throw new ServiceException("ERR-0111");
		}
		if (exam.getTeacher().getTeaId() != model.getTeaId()) {
			Teacher tea = teacherDao.getById(model.getTeaId());
			if (tea == null) {
				throw new ServiceException("ERR-0021");
			} else {
				exam.setTeacher(tea);
			}
		}
		if (exam.getTest().getTestId() != model.getTestId()) {
			Test test = testDao.getById(model.getTestId());
			if (test == null) {
				throw new ServiceException("ERR-0051");
			} else {
				exam.setTest(test);
			}
		}
		exam.setExamName(model.getExamName());
		exam.setExamClasses(model.getExamClasses());
		exam.setExamBeginDate(model.getExamBeginDate());
		exam.setExamEndDate(model.getExamEndDate());
		exam.setExamFullScore(model.getExamFullScore());
		exam.setExamPassScore(model.getExamPassScore());
		exam.setExamHour(exam.getExamHour());

		examDao.edit(exam);
		return "1";
	}

	@Override
	public String getAll(Integer teaId) {
		Teacher teacher = teacherDao.getById(teaId);
		if (null == teacher) {
			throw new ServiceException("ERR-0111");
		}
		List<Exam> list = examDao.getListByProperty("teacher.teaId", teacher.getTeaId().toString());
		
		return createExamArr(list).toString();
	}
	
	private JSONObject createExamObj(Exam exam) {
		JSONObject obj = new JSONObject();
		obj.put("examId", exam.getExamId());
		if (null != exam.getTeacher()) {
			obj.put("teaId", exam.getTeacher().getTeaId());
			obj.put("teaName", exam.getTeacher().getTeaName());
		}
		if (null != exam.getTest()) {
			obj.put("testId", exam.getTest().getTestId());
			obj.put("listenId", exam.getTest().getListen().getListenId());
		}
		obj.put("examClasses", exam.getExamClasses());
		obj.put("examName", exam.getExamName());
		obj.put("examCreateDate", DateHandle.timeGetDate(exam.getExamCreateDate()));
//		obj.put("examBeginDate", DateHandle.timeGetDate(exam.getExamBeginDate()));
//		obj.put("examEndDate", DateHandle.timeGetDate(exam.getExamEndDate()));
		obj.put("examBeginDate", exam.getExamBeginDate().toString().subSequence(0, 19));
		obj.put("examEndDate", exam.getExamEndDate().toString().subSequence(0, 19));
		obj.put("examHour", exam.getExamHour());
		obj.put("examFullScore", exam.getExamFullScore());
		obj.put("examPassScore", exam.getExamPassScore());
		
		Timestamp current = new Timestamp(System.currentTimeMillis());
		int compare = current.compareTo(exam.getExamBeginDate());
		if (compare < 0) {
			obj.put("state", 0);
		} else {
			compare = current.compareTo(exam.getExamEndDate());
			if (compare < 0) {
				obj.put("state", 1);
			} else {
				obj.put("state", 2);
			}
		}
		
		return obj;
	}
	
	private JSONArray createExamArr(List<Exam> list) {
		JSONArray jarr = new JSONArray();
		if (null != list) {
			for (Exam exam : list) {
				jarr.add(createExamObj(exam));
			}
		}
		return jarr;
	}
}
